/*    
 * This file is part of the xoperator SPARQL/XMPP agent.
 * For further information see: http://xoperator.sf.net
 * Copyright (C) 2007  Jörg Unbehauen  

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package net.sf.xoperator.ui.aiml;

import java.util.ArrayList;
import java.util.List;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.rdfxmpp.SPARQLQuery;
import net.sf.xoperator.rdfxmpp.SPARQLResult;
import net.sf.xoperator.rdfxmpp.SPARQLResultTableWrapper;
import net.sf.xoperator.store.RdfStoreInfo;
import net.sf.xoperator.ui.StringMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openbbs.blackboard.filter.KindOfFilter;
import org.openbbs.blackboard.ks.KSExecutionContext;

public class TemplateExectutionContext {

	private Log glog = LogFactory.getLog("Groovy Template Log");

	private NoPlanBlackBoardControl control;
	
	private KSExecutionContext context;

	public TemplateExectutionContext(NoPlanBlackBoardControl control, KSExecutionContext context) {
		this.control = control;
		this.context = context;
	}

	public List<SPARQLResultTableWrapper> queryForTable(String query,
			List<RdfStoreInfo> stores, List<String> agents) {
		List<SPARQLResultTableWrapper> results = new ArrayList<SPARQLResultTableWrapper>();
		SPARQLQuery squery = new SPARQLQuery(query);
		squery.setOutputType(SPARQLQuery.XML_OUTPUT);
//		if (stores != null && !stores.isEmpty()) {
//			squery.setStoresToAsk(stores);
//		}
//		if (agents != null && !agents.isEmpty()) {
//			squery.setAgentsToAsk(agents);
//		}
		squery.setStoresToAsk(stores);
		squery.setAgentsToAsk(agents);

		for (Object oresult : control.executeQuery(squery,context)) {
			SPARQLResultTableWrapper wrappedResult = new SPARQLResultTableWrapper(
					(SPARQLResult) oresult);
			results.add(wrappedResult);

		}

		return results;
	}

	public List<RdfStoreInfo> getStores() {
		List<RdfStoreInfo> configuredStores = new ArrayList<RdfStoreInfo>();
		for (Object ostore : control.getConfigurationContext().blackboard()
				.readAll(new KindOfFilter(RdfStoreInfo.class))) {
			RdfStoreInfo info = (RdfStoreInfo) ostore;
			if (!info.isObsolete()) {
				configuredStores.add(info);
			}
		}

		return configuredStores;

	}

	public Log getLog() {
		return glog;
	}
	
	
	public void sendMessageToUser(String message){
		context.blackboard().write(new StringMessage(message));
	}

}